// API - 系统消息

import { http } from "@/utils/http";
import { errorReject } from "@/utils/truncate";
import { message } from "@/utils/message";

interface ListResult extends HttpResponse {
  data?: {
    /** 列表数据 */
    list: MessageInfo[];
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
}

export type MessageInfo = {
  id: Id;
  title: string;
  description: string;
  isRead: boolean;
  createTime: string;
};

/**
 * 获取分页列表
 *
 * @param data 请求参数
 * @returns
 */
export const getMessagePageList = async (data?: object) => {
  return errorReject(
    http.request<ListResult>("post", "/messages/page", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 标记消息已读或未读
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const setMessageRead = (id: Id, data?: object) => {
  return errorReject(
    http.request<HttpResponse>("put", "/messages/" + id + "/read", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 标记消息已读（全部）
 *
 * @param data 请求参数
 * @returns
 */
export const setMessageReadAll = (data?: object) => {
  return errorReject(
    http.request<HttpResponse>("put", "/messages/read", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const delMessageDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/messages/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 批量删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const batchDelMessagesDetail = (data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/messages", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};
